#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
#define ll long long
int n,T,a[N],col[N],place[1000005];
ll ans,dp[N][2];

inline ll check(){
    ll res=0;
    for(int i=1;i<=n;i++){
        for(int j=i-1;j>=1;j--){
            if(col[j]==col[i]){
                //cout<<j<<' '<<i<<' '<<a[i]<<' '<<a[j]<<endl;
                if(a[j]==a[i]) res+=a[i];
                break;
            }
        }
    }
    return res;
}

inline void dfs(int pos){
    if(pos==n+1){
        ans=max(ans,check());
        return ;
    }
    col[pos]=1;
    dfs(pos+1);
    col[pos]=0;
    dfs(pos+1);
    return ;
}

int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            dp[i][1]=0;
            dp[i][0]=0;
            place[a[i]]=0;
            col[i]=-1;
        }
        if(n<=20){
            dfs(1);
            printf("%lld\n",ans);
        }
        else{
            for(int i=1;i<=n;i++){
                dp[i][0]=max(dp[i-1][1],dp[i-1][0]);
                dp[i][1]=max(dp[i-1][1],dp[i-1][0]);
                if(place[a[i]]!=0){
                    ll temp=0;
                    for(int j=place[a[i]]+1+1;j<=i-1;j++){
                        if(a[j]==a[j-1]) temp+=a[j];
                    }
                    dp[i][1]=max(dp[i][1],dp[ place[a[i]] ][1]+a[i]+temp);
                    dp[i][0]=max(dp[i][0],dp[ place[a[i]] ][0]+a[i]+temp);
                }
                place[a[i]]=i;
            }
            printf("%lld\n",max(dp[n][1],dp[n][0]));
        }
    }
    return 0;
}
